//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SobolBrownianGenerator.h"
using namespace Cephei::QL::Models::Marketmodels::Browniangenerators;
#include <gen/QL/Models/Marketmodels/BrownianGenerator.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (UInt64 factors, UInt64 steps, QL::Models::Marketmodels::Browniangenerators::SobolBrownianGenerator::OrderingEnum ordering, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers) : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
    try
    {
#ifdef HANDLE
        _phSobolBrownianGenerator = NULL;
#endif
        QuantLib::Size _factors = (QuantLib::Size)ValueHelper::Convert (factors); //d
        QuantLib::Size _steps = (QuantLib::Size)ValueHelper::Convert (steps); //d
        QuantLib::SobolBrownianGenerator::Ordering _ordering = (QuantLib::SobolBrownianGenerator::Ordering)ordering ;
        QuantLib::SobolRsg::DirectionIntegers _directionIntegers = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>::IsSome::get (directionIntegers) ? (QuantLib::SobolRsg::DirectionIntegers)directionIntegers->Value : QuantLib::SobolRsg::DirectionIntegers::Jaeckel); //10
        _ppSobolBrownianGenerator = new boost::shared_ptr<QuantLib::SobolBrownianGenerator> (new QuantLib::SobolBrownianGenerator ( _factors,  _steps,  _ordering,  _directionIntegers ));
        SetBrownianGenerator (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (boost::shared_ptr<QuantLib::SobolBrownianGenerator>& childNative, Object^ owner) : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGenerator = NULL;
#endif
	_ppSobolBrownianGenerator = &childNative;
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (QuantLib::SobolBrownianGenerator& childNative, Object^ owner) : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGenerator = NULL;
#endif
	_ppSobolBrownianGenerator = new boost::shared_ptr<QuantLib::SobolBrownianGenerator> (&childNative);
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
    _SobolBrownianGeneratorOwner = owner;
    _BrownianGeneratorOwner = owner;
}

Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (CSobolBrownianGenerator^ copy) : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGenerator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSobolBrownianGenerator = new boost::shared_ptr<QuantLib::SobolBrownianGenerator> (copy->GetShared());
        _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (PLATFORM::Type^ t) : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGenerator = NULL;
#endif
	if (!t->IsSubclassOf(CSobolBrownianGenerator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (QuantLib::Handle<QuantLib::SobolBrownianGenerator>& childNative, Object^ owner)  : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
	_phSobolBrownianGenerator = &childNative;
	_ppSobolBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::SobolBrownianGenerator>>(childNative.currentLink());
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
    _SobolBrownianGeneratorOwner = owner;
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (QuantLib::Handle<QuantLib::SobolBrownianGenerator> childNative)  : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
	_phSobolBrownianGenerator = &childNative;
	_ppSobolBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::SobolBrownianGenerator>>(childNative.currentLink());
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::CSobolBrownianGenerator (QuantLib::SobolBrownianGenerator childNative)  : CBrownianGenerator(CSobolBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phSobolBrownianGenerator = NULL;
#endif
	_ppSobolBrownianGenerator = new boost::shared_ptr<QuantLib::SobolBrownianGenerator> (new QuantLib::SobolBrownianGenerator (childNative));
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppSobolBrownianGenerator));
}
#endif

Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::~CSobolBrownianGenerator ()
{
    if (_ppSobolBrownianGenerator != NULL)
    {
	    delete _ppSobolBrownianGenerator;
        _ppSobolBrownianGenerator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::!CSobolBrownianGenerator ()
{
    if (_ppSobolBrownianGenerator != NULL)
    {
	    delete _ppSobolBrownianGenerator;
    }
}
QuantLib::SobolBrownianGenerator& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::GetReference ()
{
    if (_ppSobolBrownianGenerator == NULL) throw REFNEW NativeNullException ();
	return **_ppSobolBrownianGenerator;
}
boost::shared_ptr<QuantLib::SobolBrownianGenerator>& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::GetShared ()
{
    if (_ppSobolBrownianGenerator == NULL) throw REFNEW NativeNullException ();
	return *_ppSobolBrownianGenerator;
}
QuantLib::SobolBrownianGenerator* Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::GetPointer ()
{
    if (_ppSobolBrownianGenerator == NULL) throw REFNEW NativeNullException ();
	return &**_ppSobolBrownianGenerator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SobolBrownianGenerator>& Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::GetHandle ()
{
	if (_phSobolBrownianGenerator == NULL)
	{
		_phSobolBrownianGenerator = new Handle<QuantLib::SobolBrownianGenerator> (*_ppSobolBrownianGenerator);
	}
	return *_phSobolBrownianGenerator;
}
#endif
bool Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::HasNative () 
{
	return (_ppSobolBrownianGenerator != NULL);
}

Double Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::NextPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSobolBrownianGenerator)->nextPath ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::NextStep (Cephei::Core::IVector<Double>^ output)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        output->Lock();
        INativeVector<Double>^ _NCIoutput = output->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCoutput = safe_cast<CDoubleVector^>(_NCIoutput);
        std::vector<QuantLib::Real>& _output = static_cast<std::vector<QuantLib::Real>&> (_NCoutput->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSobolBrownianGenerator)->nextStep ( _output );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (output != nullptr) output->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSobolBrownianGenerator)->numberOfFactors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSobolBrownianGenerator)->numberOfSteps ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IMatrix<UInt64>^ Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::OrderedIndices::get ()
{
    try
    {
    	std::vector<std::vector<QuantLib::Size> >& _rv = (std::vector<std::vector<QuantLib::Size> >&)(*_ppSobolBrownianGenerator)->orderedIndices ( );   
        Cephei::Core::IMatrix<UInt64>^ _nrv = REFNEW CoMatrix<UInt64> (REFNEW CUInt64Matrix (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IMatrix<Double>^ Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator::Transform (Cephei::Core::IMatrix<Double>^ variates)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        variates->Lock();
        INativeMatrix<Double>^ _NCIvariates = variates->getFeature (NativeFeature::Value);
        CDoubleMatrix^ _NCvariates = safe_cast<CDoubleMatrix^>(_NCIvariates);
        std::vector<std::vector<QuantLib::Real> >& _variates = static_cast<std::vector<std::vector<QuantLib::Real> >&> (_NCvariates->GetReference ());
    	std::vector<std::vector<QuantLib::Real> > _rv = (std::vector<std::vector<QuantLib::Real> >)(*_ppSobolBrownianGenerator)->transform ( _variates );   
        Cephei::Core::IMatrix<Double>^ _nrv = REFNEW CoMatrix<Double> (REFNEW CDoubleMatrix (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (variates != nullptr) variates->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Browniangenerators::ISobolBrownianGenerator^ Cephei::QL::Models::Marketmodels::Browniangenerators::CSobolBrownianGenerator_Factory::Create (UInt64 factors, UInt64 steps, QL::Models::Marketmodels::Browniangenerators::SobolBrownianGenerator::OrderingEnum ordering, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers)
{
    return REFNEW CSobolBrownianGenerator ( factors,  steps,  ordering,  directionIntegers);
}
